﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;

namespace DAO
{
    public class User_DAO
    {
        private static QLTDTVDataContext dt = new QLTDTVDataContext();

        public static User_DTO GetUsernamePassWordByUserID(int user_id)
        {
            User_DTO user = new User_DTO();
            try
            {

                var ListColum = dt.spGetUsernamePassWordByUserID(user_id);

                foreach (var colum in ListColum)
                {
                    user.UserID = user_id;
                    user.UserName = colum.USERNAME;
                    user.PassWord = colum.PASSWORD;

                }

                return user;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static User_DTO GetUserInfomationByUserID(int user_id)
        {
            User_DTO user = new User_DTO();
            try
            {

                var ListColum = dt.spGetUserInfomationByUserID(user_id);

                foreach (var colum in ListColum)
                {
                    user.UserID = user_id;
                    user.UserName = colum.USERNAME;
                    user.PassWord = colum.PASSWORD;
                    user.Date = DateTime.Parse(colum.DATE);
                    user.Email = colum.EMAIL;
                    user.Status = (short)colum.STATUS;
                }

                return user;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool UpdatePassword(User_DTO user)
        {
            try
            {
                string flag = "";
                dt.spUpdatePassword(user.UserID, user.PassWord, ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        // Admin
        public static IEnumerable GetAllRole()
        {
            return dt.spGetAllRole().AsEnumerable();
        }

        public static bool CreateUser(User_DTO user, int kiemTraCoChonRole, int kiemTraLaNguoiTimViec, int RoleID)
        {
            try
            {
                string flag = "";
                string passWord = MyMembershipProvider.ToHashString(user.PassWord);

                dt.spCreateUser(user.UserName, passWord, user.Status, user.Date, user.Email, RoleID, kiemTraCoChonRole, kiemTraLaNguoiTimViec, ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool UpdateUser(User_DTO user)
        {
            try
            {
                string flag = "";
                string passWord = MyMembershipProvider.ToHashString(user.PassWord);

                dt.spUpdateUser(user.UserID, passWord, user.Status, user.Date, user.Email, ref flag);

                if (flag == "1")
                    return true;
                else
                    return false;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static IEnumerable GetAllUser()
        {
            return dt.spGetAllUser();
        }

        public static List<int> GetRoleUser(int userID)
        {
            var dsRoleUser = dt.spGetRoleUser(userID);

            List<int> dsRoleID = new List<int>();

            foreach (var item in dsRoleUser)
            {
                dsRoleID.Add(item.ROLEID);
            }

            return dsRoleID;
        }

        public static IEnumerable GetUserInRole(int roleID)
        {
            return dt.spGetUserInRole(roleID).AsEnumerable();
        }

        public static bool DeleteUSer(int userID)
        {
            try
            {
                string flag = "";
                dt.spDeleteUser(userID, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool DeleteAllRoleUser(int userID)
        {
            try
            {
                string flag = "";
                dt.spDeleteAllRoleUser(userID, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool DeleteRoleUser(int userID, int roleID)
        {
            try
            {
                string flag = "";
                dt.spDeleteRoleUser(userID, roleID, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool InsertRoleUser(int userID, int roleID)
        {
            try
            {
                string flag = "";
                dt.spInsertRoleUser(userID, roleID, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static bool InsertRole(string roleName, string description)
        {
            try
            {
                string flag = "";
                dt.spInsertRole(roleName, description, ref flag);

                if (flag == "1")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        public static IEnumerable TimKiemUser(string tuKhoa)
        {
            return dt.spTimKiemUser(tuKhoa);
        }
    }
}
